<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!doctype html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>管理首页</title>
<meta name="description" content="这是一个 首页面">
<meta name="keywords" content="index">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta name="apple-mobile-web-app-title" content="Amaze UI" />
<jsp:include page="../inc.jsp"></jsp:include>

</head>
<body>
	<div class="admin-content">
	 	<div class="am-cf am-padding">
      		<div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">微信菜单</strong></div>
    	</div>
    	<div class='am-u-sm-12'>
    		<button type="button" class="am-btn am-btn-primary am-btn-xs"  onclick = "updateMenu();">发布微信菜单</button>
    	</div>
    	<div id="menuId">
    	</div>
    </div>
  	
<script>
	var data;
	$(function() {
		loadMenu();
	});
	
	function loadMenu(){
		$.ajax({
			url: '${pageContext.request.contextPath}/menu/show.action',
			type: 'get',
			success: function(result){
				if (result && result.length > 0){
					$("#menuId").html("");
					for (var i=0; i < result.length; i++){
						console.log(result[i].parentId);
						if (result[i].parentId == 0){
							list = "<div class='am-u-sm-3' style='text-align:right;color: #0c79b1; font-size: 16px;'>" + result[i].name + "</div><div class='am-u-sm-9'>"+ getInfo(result[i])+ "</div>";

							for (var j=i; j<result.length; j++){
								if (result[j].parentId == result[i].id){
									list += "<div class='am-u-sm-3' style='text-align:right; clear:both'>" + result[j].name + "</div><div class='am-u-sm-9'>"+ getInfo(result[j])+ "</div>";
								}
							}
							$("#menuId").append(list);
						}
					}
				}
			}
		});
	}

	function getInfo(data){
		if (data.url)
			return data.url;
		
		if (data.info)
			return data.info;

		if (data.key)
			return data.key;
		return "";
	}

	function updateMenu(){
		$.ajax({
			url: '${pageContext.request.contextPath}/menu/send.action',
			type: 'post',
			data: data,
			success: function(result){
				console.log(result);
				if (result == 'success'){
					alert("发布成功！");
				} else {
					alert("发布失败！");
				}
			}
		});
	}
	
</script>
</body>

</html>
